﻿using Microsoft.AspNetCore.Mvc;
using RuoVea.OmiApi.SystemApp.Entitys;
using RuoVea.OmiApi.SystemApp.Service;
using RuoVea.OmiApi.SystemApp.Service.Dto;

namespace RuoVea.OmiSystem.Controllers;

/// <summary>
/// 组织机构 
/// </summary> 
[AppRoute]
public class OrganizationController : Controller
{
    private readonly SysOrgService _sysOrgService;
    private readonly SugarRepository<SysOrganization> _sysOrgRep;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sysOrgService"></param>
    /// <param name="sysOrgRep"></param>
    public OrganizationController(SysOrgService sysOrgService, SugarRepository<SysOrganization> sysOrgRep)
    {
        _sysOrgService = sysOrgService;
        _sysOrgRep = sysOrgRep;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet(nameof(Index))]
    public IActionResult Index() { return View(); }
    /// <summary>
    /// 分页获取角色列表
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpGet("Org")]
    public async Task<RestfulResult<List<SysOrganization>>> GetOrgAsync([FromQuery] OrgInput data)
    {
        RestfulResult<List<SysOrganization>> restfulResult = new() { Code = CodeStatus.OK };
        restfulResult.Data = await _sysOrgService.GetList(data);
        return restfulResult;
    }
    /// <summary>
    /// 分页获取角色列表
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<SysOrganization>>> GetPagesAsync([FromBody] OrgInput data)
    {
        RestfulResult<PageResult<SysOrganization>> restfulResult = new() { Code = CodeStatus.OK, Data = new PageResult<SysOrganization>(1, 200) };
        restfulResult.Data.Rows = await _sysOrgService.GetList(data);
        restfulResult.Data.TotalRows = restfulResult.Data.Rows?.Count() ?? 0;
        return restfulResult;
    }
    private async Task<List<long>> GetChildIdListWithSelfById(long pid)
    {
        return (await _sysOrgRep.AsQueryable().ToParentListAsync((SysOrganization u) => u.Pid, pid)).Select((SysOrganization u) => u.Pid).ToList();
    }
    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("add")]
    public async Task<RestfulResult> AddAsync(AddOrgInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            var pids = await GetChildIdListWithSelfById(data.Pid);
            pids.Reverse();
            pids.Add(data.Pid);
            //data.Pids = pids.Json2Str();
            await _sysOrgService.AddOrg(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<RestfulResult> UpdateAsync(UpdateOrgInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysOrgService.UpdateOrg(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task<RestfulResult> DeleteAsync(DeleteOrgInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysOrgService.DeleteOrg(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
}